#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
using ll = long long;

const int MAXN = 3010;
const int MOD = 998244353;

int n, a[MAXN], b[MAXN];
ll dp[MAXN][MAXN];
ll sdp[MAXN][MAXN];


int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];

    for (int i = 1; i <= n; i++) cin >> b[i];

    for (int i = a[1]; i <= b[1]; i++) {
        dp[1][i] = 1;
        sdp[1][i] = sdp[1][i-1] + 1;
    }

    for (int i = 2; i <= n; i++) {
        for (int j = a[i]; j <= b[i]; j++) {
            dp[i][j] = sdp[i-1][min(j, b[i-1])];
            sdp[i][j] = (sdp[i][j-1]+dp[i][j])%MOD;
        }
    }

    ll res = 0;
    for (int i = a[n]; i <= b[n]; i++)
        res = (res+dp[n][i]) % MOD;
    cout << res << endl;
    return 0;
}